package main;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import java.awt.Component;
import javax.swing.Box;

import com.aptech.mwg.etown.pojo.Account;
import com.aptech.mwg.etown.pojo.Company;
import com.aptech.mwg.etown.pojo.exception.InsertDBException;
import com.aptech.mwg.etown.server.business.AccountManager;
import com.aptech.mwg.etown.server.business.CompanyManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class incom extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField txtCompanyname;
    private JTextField txtBusiness;
    private JTextField txtDes;
    private JTextField txtCreatedate;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
	try {
	    incom dialog = new incom();
	    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	    dialog.setVisible(false);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * Create the dialog.
     */
    public incom() {
    	setTitle("Add new Company");
	setBounds(100, 100, 409, 191);
	getContentPane().setLayout(new BorderLayout());
	contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
	getContentPane().add(contentPanel, BorderLayout.CENTER);
	GridBagLayout gbl_contentPanel = new GridBagLayout();
	gbl_contentPanel.columnWidths = new int[]{0, 0, 0};
	gbl_contentPanel.rowHeights = new int[]{0, 0, 0, 0, 0};
	gbl_contentPanel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
	gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
	contentPanel.setLayout(gbl_contentPanel);
	{
		JLabel lblCompanyName = new JLabel("Company Name: ");
		GridBagConstraints gbc_lblCompanyName = new GridBagConstraints();
		gbc_lblCompanyName.insets = new Insets(0, 0, 5, 5);
		gbc_lblCompanyName.anchor = GridBagConstraints.WEST;
		gbc_lblCompanyName.gridx = 0;
		gbc_lblCompanyName.gridy = 0;
		contentPanel.add(lblCompanyName, gbc_lblCompanyName);
	}
	{
		txtCompanyname = new JTextField();
		GridBagConstraints gbc_txtCompanyname = new GridBagConstraints();
		gbc_txtCompanyname.insets = new Insets(0, 0, 5, 0);
		gbc_txtCompanyname.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtCompanyname.gridx = 1;
		gbc_txtCompanyname.gridy = 0;
		contentPanel.add(txtCompanyname, gbc_txtCompanyname);
		txtCompanyname.setColumns(10);
	}
	{
		JLabel lblNewLabel = new JLabel("Business: ");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 1;
		contentPanel.add(lblNewLabel, gbc_lblNewLabel);
	}
	{
		txtBusiness = new JTextField();
		GridBagConstraints gbc_txtBusiness = new GridBagConstraints();
		gbc_txtBusiness.insets = new Insets(0, 0, 5, 0);
		gbc_txtBusiness.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtBusiness.gridx = 1;
		gbc_txtBusiness.gridy = 1;
		contentPanel.add(txtBusiness, gbc_txtBusiness);
		txtBusiness.setColumns(10);
	}
	{
		JLabel lblNewLabel_1 = new JLabel("Description: ");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.anchor = GridBagConstraints.WEST;
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 0;
		gbc_lblNewLabel_1.gridy = 2;
		contentPanel.add(lblNewLabel_1, gbc_lblNewLabel_1);
	}
	{
		txtDes = new JTextField();
		GridBagConstraints gbc_txtDes = new GridBagConstraints();
		gbc_txtDes.insets = new Insets(0, 0, 5, 0);
		gbc_txtDes.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtDes.gridx = 1;
		gbc_txtDes.gridy = 2;
		contentPanel.add(txtDes, gbc_txtDes);
		txtDes.setColumns(10);
	}
	{
		JLabel lblCreateDate = new JLabel("Create Date: ");
		GridBagConstraints gbc_lblCreateDate = new GridBagConstraints();
		gbc_lblCreateDate.anchor = GridBagConstraints.WEST;
		gbc_lblCreateDate.insets = new Insets(0, 0, 0, 5);
		gbc_lblCreateDate.gridx = 0;
		gbc_lblCreateDate.gridy = 3;
		contentPanel.add(lblCreateDate, gbc_lblCreateDate);
	}
	{
		txtCreatedate = new JTextField();
		GridBagConstraints gbc_txtCreatedate = new GridBagConstraints();
		gbc_txtCreatedate.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtCreatedate.gridx = 1;
		gbc_txtCreatedate.gridy = 3;
		contentPanel.add(txtCreatedate, gbc_txtCreatedate);
		txtCreatedate.setColumns(10);
	}
	{
	    JPanel buttonPane = new JPanel();
	    buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
	    getContentPane().add(buttonPane, BorderLayout.SOUTH);
	    {
		JButton okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    CompanyManager com = new CompanyManager();
			    Company company = new Company();
			    company.setName(txtCompanyname.getText());
			    company.setBusiness(txtBusiness.getText());
			    company.setDescription(txtDes.getText());
			    String createday = txtCreatedate.getText();
			    String pattern = "dd/MM/yyyy";
			    SimpleDateFormat formmater = new SimpleDateFormat(pattern);
			    Calendar cal = Calendar.getInstance();
			    try {
				    Date date = formmater.parse(createday);
				   
				    cal.setTime(date);
				   
				    String dateStr = formmater.format(date);
				    
				    System.out.println(dateStr);
				    
				    company.setCreate_date(cal.getTime());
				    
				} catch (ParseException e1) {
				    e1.printStackTrace();
				}
				
				
				try {
				    boolean result = com.insert(company);

				    if (result == true) {
					
					setVisible(false);
					
				    } else {

				    }
				    System.out.println("Insert result:" + result);
				} catch (InsertDBException e1) {
				    System.err.println("Can not insert company by : "
					    + e1.getMessage());
				    e1.printStackTrace();
				}
				
				managercompany managercom = new managercompany();
				managercom.loadtable();
			}
		});
		okButton.setActionCommand("OK");
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);
	    }
	    {
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    setVisible(false);
			}
		});
		{
			JButton btnReset = new JButton("Reset");
			btnReset.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				    txtBusiness.setText("");
				    txtCompanyname.setText("");
				    txtCreatedate.setText("");
				    txtDes.setText("");
				    
				}
			});
			buttonPane.add(btnReset);
		}
		cancelButton.setActionCommand("Cancel");
		buttonPane.add(cancelButton);
	    }
	}
    }

}
